package com.ftg.elvis.controller;

import com.ftg.elvis.pojo.Product;
import com.ftg.elvis.service.AdminProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/product")
public class AdminProductController {

	@Autowired
	AdminProductService adminProductService;

	@GetMapping("/showAll")
	public Map<String, Object> showAll(int page, int limit, String username, String pname){

		List<Product> list =  adminProductService.showAll(username, pname, page, limit);
		Map<String, Object> map = new HashMap<>();
		map.put("pname", pname);
		map.put("code", 0);
		map.put("count", adminProductService.getCount(username, pname));
		map.put("data", list);
		return map;
	}

	@GetMapping("/getBid")
	public Map<String, Object> getBid(String username){
		Map<String, Object> map = new HashMap<>();
		String bid = adminProductService.getBid(username);
		map.put("code", 0);
		map.put("data", bid);
		return map;
	}


	@PostMapping("/add")
	public int add(Product product){
		return adminProductService.add(product);
	}

	@PostMapping("/update")
	public int update(Product product){
		return adminProductService.update(product);
	}

	@DeleteMapping("/del/{pid}")
	public int delete(@PathVariable String pid){
		return adminProductService.delete(pid);
	}
}
